/** *******************************************************************
 * FlexDatabaseManager
 * @author  Jonnie Spratley
 * @contact jonniespratley@gmail.com
 * @internal
 ******************************************************************* */
package com.jonniespratley.flexdatabasemanager.commands.help
{
	import com.adobe.cairngorm.commands.ICommand;
	import com.adobe.cairngorm.control.CairngormEvent;
	import com.jonniespratley.flexdatabasemanager.business.MySQLHelpDelegate;
	import com.jonniespratley.flexdatabasemanager.events.help.GetMySQLHelpEvent;
	import com.jonniespratley.flexdatabasemanager.factories.FlexDatabaseManagerFactory;
	import com.jonniespratley.flexdatabasemanager.model.ModelLocator;
	import com.jonniespratley.flexdatabasemanager.vo.*;
	
	import mx.controls.Alert;
	import mx.rpc.IResponder;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;

	public class GetMySQLHelpCommand implements ICommand, IResponder
	{
		private var model:ModelLocator = ModelLocator.getInstance();

		public function execute( event:CairngormEvent ) : void
		{
			model.interactingWithServer = true;
			
			var evt:GetMySQLHelpEvent = event as GetMySQLHelpEvent;
			var delegate:MySQLHelpDelegate = new MySQLHelpDelegate( this );
				delegate.getHelp();
		}

		public function result( data:Object ) : void
		{
			model.interactingWithServer = false;
			model.helpCollection = FlexDatabaseManagerFactory.parseJSONResult( data.result );	
		}

		public function fault( fault:Object ):void
		{
			model.interactingWithServer = false;
			var faultEvt:FaultEvent = fault as FaultEvent;
			Alert.show(faultEvt.fault.toString(), "Error");
			trace(faultEvt.fault.faultString);
		}

	}
}